package com.daxt.utils.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import com.daxt.model.constant.FileExt;
import com.aspose.words.Document;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ToPdfUtil {

    public static boolean getLicense() {
        boolean result = false;
        try {
//            InputStream is = ToPdfUtil.class.getClassLoader().getResourceAsStream("/sys/xlsxlicense.xml"); //  license.xml应放在..\WebRoot\WEB-INF\classes路径下
            //InputStream is = ToPdfUtil.class.getClassLoader().getResourceAsStream("/sys/wordlicense.xml"); //  license.xml应放在..\WebRoot\WEB-INF\classes路径下
//            InputStream is = new FileInputStream("F:\\workspace\\daxt-project\\daxt\\src\\main\\resources\\sys\\xlsxlicense.xml"); //  license.xml应放在..\WebRoot\WEB-INF\classes路径下
            //InputStream is = new FileInputStream("F:\\zjq\\idea\\test\\src\\main\\resources\\sys\\wordlicense.xml");
            Resource resource = new ClassPathResource("/sys/wordlicense.xml");
            InputStream is = resource.getInputStream();

            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean getWordsLicense() {
        boolean result = false;
        try {
            //InputStream is = new FileInputStream("F:\\zjq\\idea\\test\\src\\main\\resources\\sys\\wordlicense.xml");
            Resource resource = new ClassPathResource("/sys/wordlicense.xml");
            InputStream is = resource.getInputStream();

            com.aspose.words.License aposeLic = new com.aspose.words.License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @description: xlx转pdf
     *
     * @author: lig
     * @date: 2022/8/20
     */
    public static String excel2pdf(String Address,String outPath) {

        if (!getLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生
            return null;
        }
        String pdfPath = FileBaseUtil.getFileNameFront(Address);
        outPath = outPath.substring(0,outPath.lastIndexOf("." )+ 1) + FileExt.pdf.getDesc();
//        outPath = SystemConfig.tempPath + outPath.substring(0,outPath.lastIndexOf("." )+ 1) + FileExt.pdf.getDesc();
        try {
            File pdfFile = new File(outPath);// 输出路径
//            File pdfFile = new File(FileBaseUtil.builFileAllName(pdfPath, FileExt.pdf.getDesc()));// 输出路径
//            File pdfFile = new File("D:\\a\\pdf1.pdf");// 输出路径
            Workbook wb = new Workbook(Address);// 原始excel路径
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            wb.save(fileOS, SaveFormat.PDF);
            fileOS.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pdfPath + ".pdf";
    }


    public static String wordTopdf(String Address,String outPath) {

        if (!getWordsLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生
            return null;
        }
        String pdfPath = FileBaseUtil.getFileNameFront(Address);
        outPath = outPath.substring(0,outPath.lastIndexOf("." )+ 1) + FileExt.pdf.getDesc();
//        outPath = SystemConfig.tempPath + outPath.substring(0,outPath.lastIndexOf("." )+ 1) + FileExt.pdf.getDesc();
        try {
            File pdfFile = new File(outPath);// 输出路径
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            Document doc = new Document(Address); //Address是将要被转化的word文档

            doc.save(fileOS, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            fileOS.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pdfPath + ".pdf";
    }

    /**
     * @description: doc转pdf
     *
     * @author: lig
     * @date: 2022/8/20
     */
    public static void doc2pdf(String wordPath, String pdfPath) {
        if (!getWordsLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return;
        }
        try {
            long old = System.currentTimeMillis();
            File file = new File(pdfPath); //新建一个pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(wordPath); //Address是将要被转化的word文档
            doc.save(os, com.aspose.words.SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            os.close();
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒"); //转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*public static void main(String[] args) throws Exception{
//        ToPdfUtil.excel2pdf("C:\\Users\\Administrator\\Desktop\\档案系统-分类配置模板.xlsx");


//        InputStream is = ToPdfUtil.class.getClassLoader().getResourceAsStream("/sys/xlsxlicense.xml");
//        URL url = ToPdfUtil.class.getClassLoader().getResource("/");
//
//
//        File f = new File(ToPdfUtil.class.getClass().getResource("/sys/xlsxlicense.xml").getPath());
//        System.out.println("f:" + f);
//        System.out.println(url.getPath());

        //Class<?> aClass = Class.forName("com.aspose.words.zzYUI");
        //Field zzZ7J = aClass.getDeclaredField("zzZ7J");
        //zzZ7J.setAccessible(true);

        //Class<?> bClass = Class.forName("com.aspose.words.zzZOF");
        //Field zzZ7N = bClass.getDeclaredField("zzZ7N");
        //zzZ7N.setAccessible(true);
        //Field modifiersField = Field.class.getDeclaredField("modifiers");
        //modifiersField.setAccessible(true);
        //modifiersField.setInt(zzZ7N, zzZ7J.getModifiers() & ~Modifier.FINAL);



        ToPdfUtil.doc2pdf("F:\\导出mysql数据库操作步骤(2).docx","F:\\1111.pdf");

        //ToPdfUtil.excel2pdf("F:\\zjq\\材料下载文件\\a.xls","F:\\zjq\\材料下载文件\\a(有水印).pdf");
    }*/

}
